﻿using System.Reflection;

namespace Xioa_UI.DataOrigin;

public abstract class ComputerBase : IComputerValue
{
    protected abstract bool IsCompareToChange();

    public void OnPropertyChanged<T>(string name,  Action action)
    {
        action();
    }

    public void OnPropertyChanged<T>(string name, T oldValue, T newValue, Action action)
        where T : IComparable
    {
        if (!IsCompareToChange()) return;
        if (newValue.CompareTo(oldValue) == 1)
        {
            action();
        }
    }

    public TD OnPropertyChanged<T, TD>(string name, T newValue, Func<T, TD> func)
    {
        return func.Invoke(newValue);
    }

    public TD? OnPropertyChanged<T, TD>(string name, T oldValue, T newValue, Func<T, TD> func)
        where T : IComparable
        where TD : new()
    {
        if (!IsCompareToChange()) return new TD();
        if (newValue.CompareTo(oldValue) == 1)
        {
            return func.Invoke(newValue);
        }

        return new TD();
    }
}